'''
Created on 2011-1-25

@author: Q
'''

import os, ParseTheMapUsingOpenTTDExecutable, ParseSaveFileDumpResult, ConfigBase
from Configuration import Configuration
cf = Configuration()
"""
Ensure that num of experiments are correct. 
1) It ask all the client to check the autosave#.sav and dump the map
2) It uses the ParseSaveFileDumpResult to parse all the map dump result, to check whether there are 
enough dump result, if there are not enough, the difference(diff) between numRequired and actually correct one 
is report
3) It return required diff number to ResourceManager.py module and the ResourceManager.py module will run required jobs.
"""
def getErrorNum(expName, factor, numRequired,expId=0):
    try:
        diff = ParseSaveFileDumpResult.checkSaveGameCorrect(expName, factor, numRequired, expId)
        print "--------------------------------------------"
        print "There is a different %d"%diff
        print "--------------------------------------------"
        return diff 
    except :
        
        return numRequired
        pass

"""
This is invoked by client at Process.is10Years
Some errors here
"""
def clientDump(expName, filename="autosave0.sav", path="/var/tmp/sshen/run/save/autosave", prefix=""):
    return;
    if os.path.exists(cf.getAutoSavePath(expName)) == False:
        os.system("mkdir " + cf.getAutoSavePath(expName))
    cf.setRandomAI(0);
    ParseTheMapUsingOpenTTDExecutable.openTTDPath = os.path.join(ConfigBase.getBinaryDirectoryForRunPath(),"saveload");
    if(ParseTheMapUsingOpenTTDExecutable.openTTDPath.find(' ')>=0):
        ParseTheMapUsingOpenTTDExecutable.openTTDPath = '\"' + ParseTheMapUsingOpenTTDExecutable.openTTDPath + '\"';
    autosaveFilename = os.path.join(path,filename)
    dumpFilePath = os.path.join(path, "companyScore.txt")
    newName = os.path.join(path, filename + "_companyScore.txt")
    ParseTheMapUsingOpenTTDExecutable.getCompanyScore(autosaveFilename, dumpFilePath);
    os.system("mv " + newName + " " + os.path.join(cf.getAutoSavePath(expName), prefix + filename+"_companyScore.txt"))
    
if __name__ == "__main__":

    getErrorNum("1a10", 0, 30, 626)